package com.weilus.dto;

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import org.springframework.validation.BindingResult;

import java.io.Serializable;

public abstract class RespEntity implements Serializable{
	private static final long serialVersionUID = 9152335041676012427L;
	
	protected String code;
	protected String msg;
	protected Object content;

	public RespEntity() {
	}

	public RespEntity(String code, String msg, Object content) {
		this.code = code;
		this.msg = msg;
		this.content = content;
	}

	@JacksonXmlProperty(localName="code")
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	@JacksonXmlProperty(localName="msg")
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

	@JacksonXmlProperty(localName="content")
	public Object getContent() {
		return content;
	}
	public void setContent(Object content) {
		this.content = content;
	}


	public static SuccResp ok(Object content){
		return new SuccResp(content);
	}

	public static ErrorResp fail(BindingResult errResult){
		if(errResult.hasErrors()){
			return new ErrorResp(errResult.getAllErrors().get(0));
		}
		return null;
	}
	
}
